package com.hbnu.server.http;

import com.hbnu.server.http.handler.FileUploadHandler;
import com.hbnu.server.http.handler.WebPageHandler;
import com.hbnu.tools.FileTool;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;

import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import io.netty.bootstrap.ServerBootstrap;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;

import java.util.HashMap;


import static com.hbnu.tools.PrintTool.println;
import static com.hbnu.tools.ScannerTool.nextLine;

public class HttpServer {
    public HttpServer() {
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        NioEventLoopGroup boss = new NioEventLoopGroup();
        NioEventLoopGroup worker = new NioEventLoopGroup();
        serverBootstrap.group(boss, worker);
        serverBootstrap.channel(NioServerSocketChannel.class);
        serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel socketChannel) {
                socketChannel.pipeline().addLast(new HttpServerCodec());
                socketChannel.pipeline().addLast(new HttpObjectAggregator(1024 * 1024 * 1024)); // 限制为 1G
                socketChannel.pipeline().addLast(new WebPageHandler());
                socketChannel.pipeline().addLast(new FileUploadHandler());

            }
        });
        try {
           serverBootstrap.bind(8080);
            //并且阻塞方法，不让主方法结束，添加日志
            getQR();
            nextLine();
            println("传输关闭");

        }  finally {
            boss.shutdownGracefully();
            worker.shutdownGracefully();
        }
    }

    private static void getQR() {
        String content="http://"+ FileTool.getIP() +":8080/";  // 要生成二维码的网页链接
        int width = 1; // 二维码宽度
        int height = 1; // 二维码高度

        // 定义二维码的参数
        HashMap<EncodeHintType, java.io.Serializable> hints = new HashMap<EncodeHintType, java.io.Serializable>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");//编码方式
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);//纠错等级

        // 打印二维码
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            for (int j = 0; j < bitMatrix.getHeight(); j++) {
                for (int i = 0; i < bitMatrix.getWidth(); i++) {
                    if (bitMatrix.get(i, j)) {
                        System.out.print("■");
                    } else {
                        System.out.print("  ");
                    }

                }
                System.out.println();
            }
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }

}
